package com.loongtech.bi.manager.system;

import com.loongtech.bi.entity.POJO.Page;
import com.loongtech.bi.entity.system.EntityBiChannel;
import com.loongtech.bi.entity.system.EntityBiPlatform;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.loongtech.bi.manager.system.SysProjectManager.projectIdNameMap;

@Service
public class BiChannelManager extends ManagerQueryCacheBase<EntityBiChannel> {
    private static final long serialVersionUID = 2L;

    @Autowired
    private SysUserManager sysUserManager;

    public Map<String, String> getIdAndNameMap(int userId) {
        Map<String, String> map = new LinkedHashMap<>();
        List<EntityBiChannel> list = getByUserId(userId);
        //排序保证0在最前面
        Collections.sort(list, new Comparator<EntityBiChannel>() {
            @Override
            public int compare(EntityBiChannel o1, EntityBiChannel o2) {
                return o1.getId().compareTo(o2.getId());
            }
        });

        for (EntityBiChannel info : list) {
            map.put(info.getId(), info.getName());
        }
        return map;
    }

    public List<EntityBiChannel> getByUserId(int userId) {
        // 对超级用户特殊处理
        if (sysUserManager.isAdmin(userId)) {
            return super.findAll();
        }

        //根据授权处理
        List<EntityBiChannel> list = new ArrayList<>();
        Set<String> set = new HashSet<>();
        List<Object> oList = findByQuery("select pp from EntitySysUserRole as ur, EntitySysRoleChannel as rp, EntityChannel as pp where ur.roleId = rp.roleId and rp.channelId = pp.id and ur.userId =:userId", "userId", userId);
        if (null != oList && !oList.isEmpty()) {
            for (Object o : oList) {
                EntityBiChannel channel = (EntityBiChannel) o;
                if (set.add(channel.getId())) {
                    list.add(channel);
                }
            }
        }
        return list;
    }

    /**
     * 查询
     */
    public Page<List<EntityBiChannel>> searchByCriteria(Integer curPage, Integer pageSize, EntityBiChannel e) {
        List<Object> list = new ArrayList<>();
        StringBuilder hql = new StringBuilder(" from EntityBiChannel e where 1=1 ");
        if (!Utils.isEmptyStr(e.getId())) {
            hql.append(" and e.id = ? ");
            list.add(e.getId());
        }
        if (!Utils.isEmptyStr(e.getDescription())) {
            hql.append(" and e.description like ?");
            list.add("%" + e.getDescription() + "%");
        }
        if (e.getGameId() != null) {
            hql.append(" and e.gameId = ?");
            list.add(e.getGameId());
        }
        if (!Utils.isEmptyStr(e.getName())) {
            hql.append(" and e.name like ?");
            list.add("%" + e.getName() + "%");
        }
        if (!Utils.isEmptyStr(e.getPlatName())) {
            hql.append(" and e.platName like ?");
            list.add("%" + e.getPlatName() + "%");
        }
        hql.append(" order by e.gameId,id");
        Integer totalSize = countNum(hql.toString(), list.toArray());
        //数据
        List<EntityBiChannel> res = findByQuery((curPage - 1) * pageSize, pageSize, hql.toString(), list.toArray());

        List<EntityBiPlatform> clist = BiPlatformManager.PLATFORM_LIST;
        Map<String, String> map = new HashMap<>();
        if (clist != null && clist.size() > 0) {
            for (EntityBiPlatform entityBiPlatform : clist) {
                map.put(entityBiPlatform.getId(), entityBiPlatform.getName());
            }
        }

        if (res != null && res.size() > 0) {
            //取平台名,工程名
            for (EntityBiChannel channel : res) {
                String s = channel.getPlatformId();
                channel.setPlatformName(map.get(s));
                channel.setGameName(projectIdNameMap.get(channel.getGameId().toString()));
            }
        }

        return new Page<>(res, totalSize, curPage, pageSize);
    }
}
